<template>
  <section class="area-wrapper">
    <el-form :inline="true" :model="param"  class="area-wrapper_search">
      <el-form-item label="共享方案编号">
        <el-input v-model="param.schemeNo" placeholder="请输入共享方案编号" @keyup.enter.native="onQuery"></el-input>
      </el-form-item>
      <el-form-item label="共享方案名称">
        <el-input v-model="param.name" placeholder="请输入共享方案名称" @keyup.enter.native="onQuery"></el-input>
      </el-form-item>
      <el-form-item label="共享方案类型">
        <el-select v-model="param.shareSchemeType" ref="schemeType" placeholder="请选择共享方案类型">
          <el-option
            v-for="(item, index) in schemeTypelist"
            :key="index"
            :label="item.dicValue"
            :value="item.dicCode">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onQuery" type="primary">查询</el-button>
        <el-button @click="getReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button class="mtb15" type="primary" @click="addItem" v-if="this.$can('SHARE_ADD')">新增方案</el-button>
    <nb-table
      operatorText
      :tableHeaders="tableHeaders"
      :tableData="tableData"
      :fixedOne="fixedOne"
      :clickData="clickData"
      :clickLast="clickLast"
      @clickRight="clickRight"
      :format="format"
      :row="row"
      height="auto"
      operatorFixed="left">
    </nb-table>
    <el-pagination
      align="right"
      style="padding-top: 20px;margin-bottom:100px;"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageData.currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageData.pageSize"
      :total="pageData.totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </section>
</template>

<script>
  import NbTable from '@/components/nb-table.vue'
  const DEFAULT_DATA = {
    schemeNo: '',
    name: '',
    shareSchemeType: ''
  }
  export default {
    components: {
      NbTable
    },
    data () {
      return {
        param: Object.assign({}, DEFAULT_DATA),
        pageData: {
          totalPage: 0,
          pageSize: 10,
          currentPage: 1
        },
        schemeTypelist: [],
        tableHeaders: [
          {
            headerKey: 'shareSchemeNo',
            headerName: '共享方案编号'
          },
          {
            headerKey: 'name',
            headerName: '共享方案名称'
          },
          {
            headerKey: 'shareSchemeTypeName',
            headerName: '共享方案类型'
          }
        ],
        row: [],
        format: [],
        tableData: [],
        clickData: [],
        fixedOne: false,
        clickOne: false,
        clickLast: true
      }
    },
    mounted () {
      this.getConfig()
      this.clickData = this.$showBtn([
        {btnName: 'SHARE_EDIT', name: '编辑', type: 'edit'},
        {btnName: 'SHARE_DEL', name: '删除', type: 'delete'}
      ])
    },
    methods: {
      // 获取方案类型配置信息
      getConfig () {
        this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/dictionary/findDictionaryByType`, {
          type: 'main_type'
        }).then(res => {
          if (res.result) {
            this.schemeTypelist = res.data
          } else {
            this.schemeTypelist = []
          }
          this.getList()
        }).catch(() => {})
      },
      onQuery () {
        this.param.schemeNo = this.param.schemeNo && this.param.schemeNo.trim()
        this.param.name = this.param.name && this.param.name.trim()
        this.pageData.currentPage = 1
        this.getList()
      },
      getList (pa) {
        let formData = Object.assign({}, this.param, {
          page: pa || this.pageData.currentPage,
          size: this.pageData.pageSize
        })
        this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareScheme/findAllShareSchemeByConditons`, formData).then(res => {
          if (res.result) {
            this.tableData = res.data.records
            this.pageData.totalPage = res.data.total
          } else {
            this.tableData = []
            this.pageData.totalPage = 0
          }
        }).catch(() => {})
      },
      getReset () {
        this.param = Object.assign({}, DEFAULT_DATA)
        this.pageData.currentPage = 1
        this.getList()
      },
      addItem () {
        this.$router.push('/systemSettings/shareScheme/addShareScheme')
      },
      editItem (code) {
        this.$router.push(`/systemSettings/shareScheme/editShareScheme/${code}`)
      },
      deleteItem (code) {
        this.$confirm('确认删除此共享方案？', '删除确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let codes = []
          codes.push(code)
          this.$http.post(`/${this.$const.PREFIX.AUTHORITY}/shareScheme/dels`, {
            codes: codes
          }).then(res => {
            this.$message.success('操作成功')
            this.pageData.currentPage = 1
            this.getList()
          }).catch(err => {
            this.$message.error(err)
          })
        }).catch(() => {})
      },
      sizeChangeHandle (val) {
        this.pageData.pageSize = val
        this.getList()
      },
      currentChangeHandle (val) {
        this.pageData.currentPage = val
        this.getList()
      },
      delQueryModel () {},
      clickRight (data) {
        switch (data.type) {
          case 'edit':
            this.editItem(data.row.code)
            break
          case 'delete':
            this.deleteItem(data.row.code)
            break
        }
      }
    }
  }
</script>

<style lang="scss">
.area-wrapper{
  &_search{
    background: white;
    padding: 20px 0 0 20px;
  }
}
  .mtb15{
    margin: 15px 0;
  }
</style>
